package com.wangyb.wework.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.wangyb.wework.bo.WwAgentEditBo;
import com.wangyb.wework.bo.WwAgentQueryBo;
import com.wangyb.wework.service.agent.IWwAgentService;
import com.wangyb.wework.vo.WwAgentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 企业应用Controller
 *
 * @author wangyb
 * @date 2021-07-21
 */
@Api(value = "企业应用控制器", tags = {"企业应用管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wework/agent")
public class WwAgentController extends BaseController {

	private final IWwAgentService iWwAgentService;

	/**
	 * 查询企业应用列表
	 */
	@ApiOperation("查询企业应用列表")
	@PreAuthorize("@ss.hasPermi('wework:agent:list')")
	@GetMapping("/list")
	public TableDataInfo<WwAgentVo> list(@Validated WwAgentQueryBo bo) {
		return iWwAgentService.queryPageList(bo);
	}

	/**
	 * 导出企业应用列表
	 */
	@ApiOperation("导出企业应用列表")
	@PreAuthorize("@ss.hasPermi('wework:agent:export')")
	@Log(title = "企业应用", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<WwAgentVo> export(@Validated WwAgentQueryBo bo) {
		List<WwAgentVo> list = iWwAgentService.queryList(bo);
		ExcelUtil<WwAgentVo> util = new ExcelUtil<WwAgentVo>(WwAgentVo.class);
		return util.exportExcel(list, "企业应用");
	}

	/**
	 * 获取企业应用详细信息
	 */
	@ApiOperation("获取企业应用详细信息")
	@PreAuthorize("@ss.hasPermi('wework:agent:query')")
	@GetMapping("/{id}")
	public AjaxResult<WwAgentVo> getInfo(@NotNull(message = "主键不能为空")
										 @PathVariable("id") Long id) {
		return AjaxResult.success(iWwAgentService.queryById(id));
	}

	/**
	 * 新增企业应用
	 */
	@ApiOperation("新增企业应用")
	@PreAuthorize("@ss.hasPermi('wework:agent:add')")
	@Log(title = "企业应用", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody WwAgentEditBo bo) {
		return toAjax(iWwAgentService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改企业应用
	 */
	@ApiOperation("修改企业应用")
	@PreAuthorize("@ss.hasPermi('wework:agent:edit')")
	@Log(title = "企业应用", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody WwAgentEditBo bo) {
		return toAjax(iWwAgentService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除企业应用
	 */
	@ApiOperation("删除企业应用")
	@PreAuthorize("@ss.hasPermi('wework:agent:remove')")
	@Log(title = "企业应用" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iWwAgentService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
